package dao.impl;

import dao.BusinessDao;
import model.Business;
import utils.DbUtil;

import java.util.List;

public class BusinessDaoImpl implements BusinessDao {
    @Override
    public List<Business> listBusiness(String businessName, String businessAddress) {
        if(businessName==null){
            businessName="";
        }
        if(businessAddress==null){
            businessAddress="";
        }
        return DbUtil.select(Business.class,"business_name like ? and business_address like ?",
                "%"+businessName+"%","%"+businessAddress+"%");
    }

    @Override
    public int saveBusiness(String businessName) {
        Business business = new Business();
        business.setBusinessName(businessName);
        return DbUtil.insertSelective(business);
    }

    @Override
    public int removeBusiness(int businessId) {
        return DbUtil.delete(Business.class,"business_id=?",businessId);
    }

    @Override
    public Business getBusinessByIdByPass(Integer businessId, String password) {
        List<Business> select = DbUtil.select(Business.class, "business_id=? and password=?", businessId, password);
        if(select!=null&&select.size()>0){
            return select.get(0);
        }
        return null;
    }

    @Override
    public Business getBusinessById(Integer businessId) {
        List<Business> list = DbUtil.select(Business.class, "business_id=?", businessId);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateBusiness(Business business) {
        Integer businessId = business.getBusinessId();
        business.setBusinessId(null);

        //update business set xxxx=?,xxxxx=? .... where business_id=?

        return DbUtil.update(business,"business_id=?",businessId);
    }

    @Override
    public int updateBusinessByPassword(Integer businessId, String password) {
        Business business=new Business();
        business.setPassword(password);
        return DbUtil.update(business,"business_id=?", businessId);
    }
}
